package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.SalesUserDAO;
import com.ecoolex.hotel.saas.entity.SalesUser;
import com.ecoolex.hotel.saas.entity.SalesUser.Column;
import com.ecoolex.hotel.saas.entity.SalesUserExample;
import com.ecoolex.hotel.saas.entity.SalesUserExample.Criteria;
import com.ecoolex.hotel.saas.pojo.QuerySalesUserDTO;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SalesUserManager {
	
    @Autowired
    private SalesUserDAO dao;
    
    @Autowired
    private SalesUserWalletManager salesUserWalletManager;

    public List<SalesUser> queryByDepartmentId(Integer departmentId){
    	SalesUserExample example = new SalesUserExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDepartmentIdEqualTo(departmentId).andDeletedEqualTo(SalesUser.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public void deleteByDepartmentId(Integer departmentId, String updateUser) {
    	queryByDepartmentId(departmentId).forEach(s -> {
    		s.setDeleted(SalesUser.IS_DELETED);
    		s.setUpdateUser(updateUser);
    		s.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(s, Column.deleted,Column.updateUser,Column.updateTime);
    	});
    }
    
    public SalesUser get(Integer id) {
		return dao.selectByPrimaryKeyWithLogicalDelete(id, SalesUser.NOT_DELETED);
	}

	public Pagination<SalesUser> query(QuerySalesUserDTO query, Page page) {
		return Pagination.create(page, 
			dao.count(query),
			() -> dao.query(query,page));
	}
	
	public Long countByDepartmentId(Integer departmentId) {
		SalesUserExample example = new SalesUserExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDepartmentIdEqualTo(departmentId).andDeletedEqualTo(SalesUser.NOT_DELETED);
		return dao.countByExample(example);
	}

	public SalesUser add(SalesUser add, String createUser){
		SalesUser old = getByUserIdAndStoreId(add.getUserId(),add.getStoreId());
		if(Check.notNull(old)) {
			//传入了部门Id
			if(Check.notNullAndGtZero(add.getDepartmentId())) {
				old.setDepartmentId(add.getDepartmentId());
				old.setIsSales(true);
				old.setRealName(add.getRealName());
				old.setPhone(add.getPhone());
				old.setPosition(add.getPosition());
			
				old.setUpdateUser(createUser);
				old.setUpdateTime(Date.from(Instant.now()));
				dao.updateByPrimaryKeySelective(old, Column.excludes(Column.deleted,Column.createUser,Column.createTime,Column.storeId,Column.userId));
			}
			if(old.getIsSales()){
				//销售员初始化钱包
				salesUserWalletManager.create(add.getStoreId(), add.getUserId(), createUser);
			}
			return old;
		}else {
			add.setIsSales(Check.notNullAndGtZero(add.getDepartmentId()));
			add.setDeleted(SalesUser.NOT_DELETED);
			add.setCreateUser(createUser);
			add.setCreateTime(Date.from(Instant.now()));
			add.setUpdateUser(createUser);
			add.setUpdateTime(Date.from(Instant.now()));
			dao.insert(add);
			if(add.getIsSales()) {
				//销售员初始化钱包
				salesUserWalletManager.create(add.getStoreId(), add.getUserId(), createUser);
			}
			return add;
		}
	}

	public SalesUser edit(SalesUser edit, String updateUser){
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.excludes(Column.deleted,Column.createUser,Column.createTime,Column.storeId,Column.userId));
		return edit;
	}
	
	public SalesUser getByUserIdAndStoreId(Integer userId, Integer storeId) {
		SalesUserExample example = new SalesUserExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andUserIdEqualTo(userId).andStoreIdEqualTo(storeId).andDeletedEqualTo(SalesUser.NOT_DELETED);
    	return dao.selectOneByExample(example);
	}
}